<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>04猜数字练习</title>
</head>
<body>
<h1>已生成一个1-100之间的随机整数</h1>
<input type="text" id="sj1">
<button onclick="guess()">点我验证</button>
<h3>小提示:<span></span></h3>
<script>
    const shu=parseInt(Math.random()*100+1);
    console.log(shu);
    var count=0;
    function guess(){
        count++;
        let input1=document.getElementById('sj1');
        let x=parseInt(input1.value);
        //isNaN()用于判断参数是否为非数字,如果是非数字,结果为true,否则为false
        if(isNaN(x)){
            alert('请输入数字');
            return;
        }
        console.log(x);
        let spanE=document.querySelector('span');
        if(x>shu){
            spanE.innerHTML='猜大了';
        }
        if(x<shu){
            spanE.innerHTML='猜小了';
        }
        if(x==shu){
            spanE.innerHTML='恭喜你用了'+count+'次就猜对了';
        }
    }
</script>
</body>
</html>